{extend name='base' /}

{block name='main'}
<div class="main-panel">
    <div class="content">
        <div class="page-inner">
            <div class="row">
                <div class="col-md-12">
                    <div class="card">
                        <div class="card-header">
                            {MyTag:auth rule="Rule/add"}<a href="{:url(\'add\')}" class="btn btn-primary btn-sm">添加</a>{/MyTag:auth}
                        </div>
                        <div class="card-body">
                            <div class="table-responsive">
                                <table id="basic-datatables" class="display table table-striped table-hover"></table>
                            </div>
                        </div>
                    </div>

                </div>
            </div>
        </div>
    </div>
</div>
{/block}

{block name='js'}
<script>
    var dataTable = $('#basic-datatables').DataTable({
        autoWidth: false,
        ajax: "{:url('rule/lst')}",
        paging: false,
        // serverSide: true,
        ordering: false,
        info: false,
        searching: false,
        columns: [
            {data: "id", title: "ID"},
            {data: "title", title: "名称", render: function (data, type, row) {
                return "|--" + "--------".repeat(row.level) + data;
            }},
            {data: "name", title: "地址"},
            {data: "menu", title: "类型", render: function (data, type, row) {
                if (data == 1) {
                    return '<button type="button" class="btn btn-primary btn-xs" id="">菜单</button>';
                }
                return '<button type="button" class="btn btn-warning btn-xs" id="">按钮</button>';
            }},
            {data: "status", title: "状态", render: function (data, type, row) {
                if (data == 1) {
                    return '<button type="button" class="btn btn-success btn-xs" id="">启用</button>';
                }
                return '<button type="button" class="btn btn-danger btn-xs" id="">禁止</button>';
            }},
            {data: "sort", title: "排序"},
            {
                "data": "id", "title": "操作", width: 190,"render": function (data, type, row) {
                    return '{MyTag:auth rule="Rule/child"}<a href="{:url(\'add\')}?id='+data+'" data-toggle="tooltip" title="" class="btn btn-link btn-success pr-0" data-original-title="Edit Task">加子级</a>{/MyTag:auth}' + '{MyTag:auth rule="Rule/edit"}<a href="{:url(\'edit\')}?id='+data+'" data-toggle="tooltip" title="" class="btn btn-link btn-primary pr-0" data-original-title="Edit Task">编辑</a>{/MyTag:auth}' + '{MyTag:auth rule="Rule/delete"}<button type="button" onclick="del('+data+')" class="btn btn-link btn-danger">删除</button>{/MyTag:auth}';
                }
            }
        ]
    });

    function del(id) {
        swal({
            title: '提示',
            text: "你确定要删除此项吗？",
            type: 'error',
            buttons:{
                cancel: {
                    visible: true,
                    text : '取消',
                    className: 'btn btn-danger'
                },
                confirm: {
                    text : '确定',
                    className : 'btn btn-success'
                }
            }
        }).then((willDelete) => {
            if (willDelete) {
               $.get("{:url('rule/del')}?id="+id, function (res) {
                   if(res.code == 1) {
                       dataTable.ajax.reload();
                   }else{
                       swal("提示", res.msg, {
                           icon : "error",
                           buttons: {
                               confirm: {
                                   className : 'btn btn-danger'
                               }
                           },
                       });
                   }
               }, 'json');
            }
        });
    }
</script>
{/block}